package utils

import "fmt"

type FamilyAccount struct {
	logout         bool
	choice         string
	initBalance    float64
	accountDetails []accountDetails
}

type accountDetails struct {
	accountType string
	balance     float64
	money       float64
	remark      string
}

func NewFamilyAccount(initBalance float64) *FamilyAccount {
	return &FamilyAccount{
		logout:         false,
		choice:         "",
		initBalance:    initBalance,
		accountDetails: make([]accountDetails, 0, 0),
	}
}

func (account *FamilyAccount) Start() {
	for !account.logout {
		account.showMainAndSelect()
		switch account.choice {
		case "1":
			account.showAccount()
		case "2":
			account.recordRevenue()
		case "3":
			account.recordExpenditure()
		case "4":
			account.checkLogout()
		default:
			fmt.Println("选择的功能不存在")
		}
	}
}

func (account *FamilyAccount) showMainAndSelect() {
	fmt.Println("--------------------家庭收支记账软件--------------------")
	fmt.Println("                    1、收支明细")
	fmt.Println("                    2、登记收入")
	fmt.Println("                    3、登记支出")
	fmt.Println("                    4、退    出")
	fmt.Print("                    请选择(1-4)：")
	fmt.Scanf("%s", &account.choice)
}

func (account *FamilyAccount) showAccount() {
	fmt.Println("--------------------当前收支明细记录--------------------")
	if account.existDetails() {
		fmt.Println("\t收支\t账户金额\t收支金额\t说明")
		for _, detail := range account.accountDetails {
			fmt.Println(detail)
		}
	} else {
		fmt.Println("当前没有收支明细，快来记一笔吧！")
	}
	fmt.Println("--------------------------------------------------------")
}

func (account *FamilyAccount) recordExpenditure() {
	var money float64
	var remark string
	var lastAccount = account.getLast()
	fmt.Println("--------------------登记支出--------------------")
	fmt.Print("本次支出金额：")
	fmt.Scanf("%f", &money)
	fmt.Print("本次支出说明：")
	fmt.Scanf("%s", &remark)
	if money > lastAccount.balance {
		fmt.Println("余额不足：", lastAccount.balance)
		return
	}
	account.accountDetails = append(account.accountDetails, accountDetails{
		accountType: "支出",
		balance:     lastAccount.balance - money,
		money:       money,
		remark:      remark,
	})
	fmt.Println("--------------------------------------------------------")
}

func (account *FamilyAccount) recordRevenue() {
	var money float64
	var remark string
	fmt.Println("--------------------登记收入--------------------")
	fmt.Print("本次收入金额：")
	fmt.Scanf("%f", &money)
	fmt.Print("本次收入说明：")
	fmt.Scanf("%s", &remark)
	account.accountDetails = append(account.accountDetails, accountDetails{
		accountType: "收入",
		balance:     account.getLast().balance + money,
		money:       money,
		remark:      remark,
	})
	fmt.Println("--------------------------------------------------------")
}

func (account *FamilyAccount) checkLogout() {
	var confirm string
	fmt.Print("你确定要退出吗？y/n: ")
	fmt.Scanf("%s", &confirm)
	switch confirm {
	case "y":
		account.logout = true
	case "n":
		account.logout = false
	default:
		fmt.Println("输入错误，请检查")
		account.checkLogout()
	}
}

func (account *FamilyAccount) getLast() accountDetails {
	if len(account.accountDetails) > 0 {
		return account.accountDetails[len(account.accountDetails)-1]
	} else {
		return accountDetails{balance: account.initBalance}
	}
}

func (account *FamilyAccount) existDetails() bool {
	return len(account.accountDetails) != 0
}

func (detail accountDetails) String() string {
	return fmt.Sprintf("\t%s\t%0.2f\t%0.2f\t%s\n", detail.accountType, detail.balance, detail.money, detail.remark)
}
